package com.udaye.movie.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.udaye.library.pullloadlibrary.CommonViewHolder;
import com.udaye.library.pullloadlibrary.RecyclerViewCommonAdapter;
import com.udaye.movie.R;
import com.udaye.movie.entity.UsBoxBean;
import com.udaye.movie.ui.main.MovieDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 北美排行榜
 * <p/>
 * Created  on 16/7/12.
 */
public class BoxAdapter extends RecyclerViewCommonAdapter<UsBoxBean.SubjectsBean> {

    private static final int FIRST = 1;
    private static final int SECOND = 2;
    private static final int THIRD = 3;

    private static final String GOLD = "#D9D919";
    private static final String SILVERY = "#D9D919";
    private static final String COPPER = "#B87333";

    public static final String[] RANK = {"th", "1st", "2nd", "3rd"};

    /**
     * @param mContext 上下文对象
     * @param list     数据List
     */
    public BoxAdapter(Context mContext, List<UsBoxBean.SubjectsBean> list) {
        super(mContext, list, R.layout.item_box_layout);
    }
    public void update(List<UsBoxBean.SubjectsBean> list) {
        addList((ArrayList<UsBoxBean.SubjectsBean>) list);
    }
    @Override
    public void onListBindViewHolder(CommonViewHolder holder, int position) {
//        final UsBoxBean.SubjectsBean subjectsBean = getItem(position);


//        holder.setText(R.id.tv_rank, String.valueOf(subjectsBean.getRank()));
//        holder.setText(R.id.tv_movie_name, String.valueOf(subjectsBean.getSubject().getTitle()));
//        holder.setText(R.id.tv_movie_score, String.format(mContext.getString(R.string.tip_score), String.valueOf(subjectsBean.getSubject().getRating().getAverage())));

        final UsBoxBean.SubjectsBean subject = getItem(position);
        int rank = subject.getRank();
        TextView text_rank = holder.itemView.findViewById(R.id.tv_item_box_rank);
        text_rank.setTextColor(getRankTextColor(rank));
        if (rank < 4) {
            text_rank.setText(RANK[rank]);
        } else {
            text_rank.setText(String.format("%d%s", rank, RANK[0]));
        }
        ImageView image_isNew = holder.itemView.findViewById(R.id.iv_item_box_isNew);
        image_isNew.setVisibility(subject.isNewX() ? View.VISIBLE : View.GONE);

        ImageView image_film = holder.itemView.findViewById(R.id.iv_item_box_image);
        TextView text_title= holder.itemView.findViewById(R.id.tv_item_box_title);
        TextView text_no_rating= holder.itemView.findViewById(R.id.tv_item_box_noRating);
        RatingBar ratingBar= holder.itemView.findViewById(R.id.rb_item_box_rating);
        TextView text_rating= holder.itemView.findViewById(R.id.tv_item_box_rating);
        LinearLayout layout_rating= holder.itemView.findViewById(R.id.ll_item_box_rating);

        UsBoxBean.SubjectsBean.SubjectBean simSubject = subject.getSubject();
        float rating = (float) simSubject.getRating().getAverage();
        if (rating == 0) {
            text_no_rating.setVisibility(View.VISIBLE);
            layout_rating.setVisibility(View.GONE);
        } else {
            ratingBar.setRating(rating / 2);
            text_rating.setText(String.format("%s", rating));
        }
        text_title.setText(simSubject.getTitle());
        Glide.with(holder.itemView.getContext())
                .load(simSubject.getImages().getLarge())
                .into(image_film);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(MovieDetailActivity.getCallingIntent(mContext, subject.getSubject().getId()));
            }
        });

    }

    private int getRankTextColor(int rank) {
        switch (rank) {
            case FIRST:
                return Color.parseColor(GOLD);
            case SECOND:
                return Color.parseColor(SILVERY);
            case THIRD:
                return Color.parseColor(COPPER);
            default:
                return Color.GRAY;
        }
    }
}